﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Data.OracleClient;
namespace GSLMS.DAL.Oracle
{
	/// <summary>
	/// 数据访问类:ADU_SPECAIL
	/// </summary>
	public partial class ADU_SPECAIL : BaseDAL<GSLMS.Entity.TB_ADU_SPECAIL>
	{
		private readonly GSLMS.Core.Loger log = new Core.Loger();
		public ADU_SPECAIL()
		{}
		#region  Method
		/// <summary>
		///  增加一条数据
		/// </summary>
		public override bool Add(GSLMS.Entity.TB_ADU_SPECAIL model)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_LOANID", model.LOANID),
								new OracleParameter("P_PROID", model.PROID),
								new OracleParameter("P_CONID", model.CONID),
								new OracleParameter("P_PID", model.PID),
								new OracleParameter("P_SPECIALTYPE", model.SPECIALTYPE),
								new OracleParameter("P_SUBMITDEPART", model.SUBMITDEPART),
								new OracleParameter("P_SUBMITUSER", model.SUBMITUSER),
								new OracleParameter("P_AUDITTIME", model.AUDITTIME),
								new OracleParameter("P_AUDITSTATUS", model.AUDITSTATUS),
								new OracleParameter("P_REMARK", model.REMARK),
								new OracleParameter("P_TODEPART", model.TODEPART),
								new OracleParameter("P_TOUSER", model.TOUSER)};
  
			for (int i = 0; i < 13; i++)
			{
				parms[i].Direction = ParameterDirection.Input;
			}

			try
			{
				OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_adu_specail_add", parms);
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///  更新一条数据
		/// </summary>
		public override bool Update(GSLMS.Entity.TB_ADU_SPECAIL model)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_LOANID", model.LOANID),
								new OracleParameter("P_PROID", model.PROID),
								new OracleParameter("P_CONID", model.CONID),
								new OracleParameter("P_PID", model.PID),
								new OracleParameter("P_SPECIALTYPE", model.SPECIALTYPE),
								new OracleParameter("P_SUBMITDEPART", model.SUBMITDEPART),
								new OracleParameter("P_SUBMITUSER", model.SUBMITUSER),
								new OracleParameter("P_AUDITTIME", model.AUDITTIME),
								new OracleParameter("P_AUDITSTATUS", model.AUDITSTATUS),
								new OracleParameter("P_REMARK", model.REMARK),
								new OracleParameter("P_TODEPART", model.TODEPART),
								new OracleParameter("P_TOUSER", model.TOUSER)};
  
			for (int i = 0; i < 13; i++)
			{
				parms[i].Direction = ParameterDirection.Input;
			}

			try
			{
				OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_adu_specail_update", parms);
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///  删除一条数据
		/// </summary>
		public override bool Delete(long ID)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", ID)};
  
				parms[0].Direction = ParameterDirection.Input;

			try
			{
				OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_adu_specail_delete", parms);
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///DataRowToModel 
		/// </summary>
		public GSLMS.Entity.TB_ADU_SPECAIL DataRowToModel(DataRow dr)
		{
			if (dr != null && dr.ItemArray.Length > 0)
			{
				GSLMS.Entity.TB_ADU_SPECAIL model = new GSLMS.Entity.TB_ADU_SPECAIL();
				if(dr["ID"]!=null && dr["ID"].ToString()!="")
				{
					model.ID=OracleHelper.ToLong(dr["ID"].ToString());
				}
				if(dr["LOANID"]!=null && dr["LOANID"].ToString()!="")
				{
					model.LOANID=OracleHelper.ToLong(dr["LOANID"].ToString());
				}
				if(dr["PROID"]!=null && dr["PROID"].ToString()!="")
				{
					model.PROID=OracleHelper.ToLong(dr["PROID"].ToString());
				}
				if(dr["CONID"]!=null && dr["CONID"].ToString()!="")
				{
					model.CONID=OracleHelper.ToLong(dr["CONID"].ToString());
				}
				if(dr["PID"]!=null && dr["PID"].ToString()!="")
				{
					model.PID=OracleHelper.ToLong(dr["PID"].ToString());
				}
				if(dr["SPECIALTYPE"]!=null && dr["SPECIALTYPE"].ToString()!="")
				{
					model.SPECIALTYPE=dr["SPECIALTYPE"].ToString();
				}
				if(dr["SUBMITDEPART"]!=null && dr["SUBMITDEPART"].ToString()!="")
				{
                    model.SUBMITDEPART = OracleHelper.ToInt(dr["SUBMITDEPART"].ToString());
				}
				if(dr["SUBMITUSER"]!=null && dr["SUBMITUSER"].ToString()!="")
				{
					model.SUBMITUSER=dr["SUBMITUSER"].ToString();
				}
				if(dr["AUDITTIME"]!=null && dr["AUDITTIME"].ToString()!="")
				{
					model.AUDITTIME=DateTime.Parse(dr["AUDITTIME"].ToString());
				}
				if(dr["AUDITSTATUS"]!=null && dr["AUDITSTATUS"].ToString()!="")
				{
                    model.AUDITSTATUS = OracleHelper.ToInt(dr["AUDITSTATUS"].ToString());
				}
				if(dr["REMARK"]!=null && dr["REMARK"].ToString()!="")
				{
					model.REMARK=dr["REMARK"].ToString();
				}
				if(dr["TODEPART"]!=null && dr["TODEPART"].ToString()!="")
				{
                    model.TODEPART = OracleHelper.ToLong(dr["TODEPART"].ToString());
				}
				if(dr["TOUSER"]!=null && dr["TOUSER"].ToString()!="")
				{
					model.TOUSER=dr["TOUSER"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

        /// <summary>
        ///DataRowToModel 
        /// </summary>
        public GSLMS.Entity.TB_ADU_SPECAIL DataRowToSpecailModel(DataRow dr)
        {
            if (dr != null && dr.ItemArray.Length > 0)
            {
                GSLMS.Entity.TB_ADU_SPECAIL model = new GSLMS.Entity.TB_ADU_SPECAIL();
                if (dr["ID"] != null && dr["ID"].ToString() != "")
                {
                    model.ID = OracleHelper.ToLong(dr["ID"].ToString());
                }
                if (dr["LOANID"] != null && dr["LOANID"].ToString() != "")
                {
                    model.LOANID = OracleHelper.ToLong(dr["LOANID"].ToString());
                }
                if (dr["PROID"] != null && dr["PROID"].ToString() != "")
                {
                    model.PROID = OracleHelper.ToLong(dr["PROID"].ToString());
                }
                if (dr["CONID"] != null && dr["CONID"].ToString() != "")
                {
                    model.CONID = OracleHelper.ToLong(dr["CONID"].ToString());
                }
                if (dr["PID"] != null && dr["PID"].ToString() != "")
                {
                    model.PID = OracleHelper.ToLong(dr["PID"].ToString());
                }
                if (dr["SPECIALTYPE"] != null && dr["SPECIALTYPE"].ToString() != "")
                {
                    model.SPECIALTYPE = dr["SPECIALTYPE"].ToString();
                }
                if (dr["SUBMITDEPART"] != null && dr["SUBMITDEPART"].ToString() != "")
                {
                    model.SUBMITDEPART = OracleHelper.ToInt(dr["SUBMITDEPART"].ToString());
                }
                if (dr["SUBMITUSER"] != null && dr["SUBMITUSER"].ToString() != "")
                {
                    model.SUBMITUSER = dr["SUBMITUSER"].ToString();
                }
                if (dr["AUDITTIME"] != null && dr["AUDITTIME"].ToString() != "")
                {
                    model.AUDITTIME = DateTime.Parse(dr["AUDITTIME"].ToString());
                }
                if (dr["AUDITSTATUS"] != null && dr["AUDITSTATUS"].ToString() != "")
                {
                    model.AUDITSTATUS = OracleHelper.ToInt(dr["AUDITSTATUS"].ToString());
                }
                if (dr["REMARK"] != null && dr["REMARK"].ToString() != "")
                {
                    model.REMARK = dr["REMARK"].ToString();
                }
                if (dr["TODEPART"] != null && dr["TODEPART"].ToString() != "")
                {
                    model.TODEPART = OracleHelper.ToLong(dr["TODEPART"].ToString());
                }
                if (dr["TOUSER"] != null && dr["TOUSER"].ToString() != "")
                {
                    model.TOUSER = dr["TOUSER"].ToString();
                }

                if (dr["CUSTNAME"] != null && dr["CUSTNAME"].ToString() != "")
                {
                    model.CUSTNAME = dr["CUSTNAME"].ToString();
                }

                if (dr["SALESMAN"] != null && dr["SALESMAN"].ToString() != "")
                {
                    model.SALESMAN = dr["SALESMAN"].ToString();
                }

                if (dr["PRODUCTNAME"] != null && dr["PRODUCTNAME"].ToString() != "")
                {
                    model.PRODUCTNAME = dr["PRODUCTNAME"].ToString();
                }

                if (dr["REGINFOID"] != null && dr["REGINFOID"].ToString() != "")
                {
                    model.REGINFOID = OracleHelper.ToLong(dr["REGINFOID"].ToString());
                }

                if (dr["SALESMANID"] != null && dr["SALESMANID"].ToString() != "")
                {
                    model.SALESMANID = OracleHelper.ToLong(dr["SALESMANID"].ToString());
                }

                if (dr["systemid"] != null && dr["systemid"].ToString() != "")
                {
                    model.SystemID = OracleHelper.ToInt(dr["systemid"].ToString());
                }

                if (dr["PRODUCTID"] != null && dr["PRODUCTID"].ToString() != "")
                {
                    model.PRODUCTID = OracleHelper.ToLong(dr["PRODUCTID"].ToString());
                }
                if (dr["CREATEDATE"] != null && dr["CREATEDATE"].ToString() != "")
                {
                    model.CREATEDATE = DateTime.Parse(dr["CREATEDATE"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public override GSLMS.Entity.TB_ADU_SPECAIL GetByID(long ID)
		{
			GSLMS.Entity.TB_ADU_SPECAIL sModel = new GSLMS.Entity.TB_ADU_SPECAIL();
			string sql = "";
			sql = "select * from TB_ADU_SPECAIL where id=" + ID.ToString();
			DataSet ds = OracleHelper.ExecuteDataset(sql);
			if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
			{
				sModel = DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
			return null;
			}
			return sModel;
		}

        /// <summary>
        /// 分页提取特批申请审批数据
        /// </summary>
        public List<GSLMS.Entity.TB_ADU_SPECAIL> GetSpecailList(GSLMS.Entity.TB_ADU_SPECAIL searchParam)
        {
            var list = new List<GSLMS.Entity.TB_ADU_SPECAIL>();
            DataTable dt = null;
            string tblName = "V_app_specialinfo_ADU";
            string sqlWhere = "";
            string strSort = "";
            int pageSize = 20;
            int pageIndex = 1;
            string strFields = "*";
            int totalCount = 0;
            int countFlag = 1;

            //是否计算总数
            countFlag = ( searchParam.ReCount) ? 1 : 0;
            pageSize = (searchParam.PageSize > 0) ? searchParam.PageSize : 15;
            pageIndex = (searchParam.CurrentIndex > 0) ? searchParam.CurrentIndex : 1;

            //查询条件 客户名称 申请时间 状态

            sqlWhere += " and AUDITSTATUS>0";
            
            if (searchParam.CUSTNAME != null && searchParam.CUSTNAME.Trim() != "")
            {
                sqlWhere += " and custname like '%" + OracleHelper.ReplaceUnsafeSql(searchParam.CUSTNAME) + "%'";
            }

            if (searchParam.SALESMAN != null && searchParam.SALESMAN.Trim() != "")
            {
                sqlWhere += " and SALESMAN like '%" + OracleHelper.ReplaceUnsafeSql(searchParam.SALESMAN) + "%'";
            }

            if (searchParam.PRODUCTID>0)
            {
                sqlWhere += " and PRODUCTID=" + searchParam.PRODUCTID.ToString();
            }

            if (searchParam.SystemID > 0)
            {
                sqlWhere += " and systemid=" + searchParam.SystemID.ToString();
            }

            if (searchParam.SUBMITUSER != null && searchParam.SUBMITUSER.Trim() != "")
            {
                sqlWhere += " and SUBMITUSER='" + OracleHelper.ReplaceUnsafeSql(searchParam.SUBMITUSER) + "'";
            }

            if (searchParam.AUDITTIME != null && searchParam.AUDITTIME.ToString().Trim() != "")
            {
                sqlWhere += " and AUDITTIME>=to_date('" + Convert.ToDateTime(searchParam.AUDITTIME).ToString("yyyy-MM-dd HH:mm:ss") + "','yyyy-mm-dd hh24:mi:ss')";
            }

            if (searchParam.AUDITTIME_END != null && searchParam.AUDITTIME_END.ToString().Trim() != "")
            {
                sqlWhere += " and AUDITTIME<to_date('" + Convert.ToDateTime(searchParam.AUDITTIME_END).AddDays(1).AddSeconds(-1).ToString("yyyy-MM-dd HH:mm:ss") + "','yyyy-mm-dd hh24:mi:ss')";
            }

            



            dt = OracleHelper.GetPageData(tblName, strFields, strSort, pageSize, pageIndex, countFlag, sqlWhere, out totalCount);

            searchParam.RowCount = totalCount;
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    GSLMS.Entity.TB_ADU_SPECAIL model = new GSLMS.Entity.TB_ADU_SPECAIL();
                    model = DataRowToSpecailModel(dt.Rows[i]);
                    list.Add(model);
                }
            }
            else
            {
                return null;
            }
            return list;
        }

        /// <summary>
        /// 通过特批业务类型和父ID提取已审批信息
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="specialType"></param>
        /// <returns></returns>
        public List<GSLMS.Entity.TB_ADU_SPECAIL> GetAllByTypeAndPID(long pid, int specialType)
        {
            var list = new List<GSLMS.Entity.TB_ADU_SPECAIL>();
            DataTable dt = null;
            string sql = "select * from TB_Adu_Specail where SPECIALTYPE="+specialType.ToString()+" and pid="+pid.ToString()+" and AUDITSTATUS>0 order by AUDITTIME asc";

            DataSet ds = OracleHelper.ExecuteDataset(sql);
            if (ds != null && ds.Tables.Count > 0)
                dt = ds.Tables[0];

            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    GSLMS.Entity.TB_ADU_SPECAIL model = new GSLMS.Entity.TB_ADU_SPECAIL();
                    model = DataRowToModel(dt.Rows[i]);
                    list.Add(model);
                }
            }
            else
            {
                return null;
            }

            return list;
        }

		/// <summary>
		/// 分页提取数据
		/// </summary>
		public override List<GSLMS.Entity.TB_ADU_SPECAIL> GetList(GSLMS.Entity.TB_ADU_SPECAIL searchParam)
		{
			var list=new List<GSLMS.Entity.TB_ADU_SPECAIL>();
			DataTable dt = null;
			string tblName = "TB_ADU_SPECAIL";
			string sqlWhere="";
			string strSort="";
			int pageSize=20;
			int pageIndex=1;
			string strFields="*";
			int totalCount = 0;
			int countFlag = 1;
			dt = OracleHelper.GetPageData(tblName, strFields, strSort, pageSize, pageIndex,countFlag, sqlWhere, out totalCount);
			if (dt != null && dt.Rows.Count > 0)
			{
				for (int i = 0; i < dt.Rows.Count; i++)
				{
					GSLMS.Entity.TB_ADU_SPECAIL model = new GSLMS.Entity.TB_ADU_SPECAIL();
					model = DataRowToModel(dt.Rows[i]);
					list.Add(model);
				}
			}
			else
			{
				return null;
			}
			return list;
		}


        /// <summary>
        /// 特批申请部门审批
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SpecailBMAudit(GSLMS.Entity.TB_ADU_SPECAIL model)
        {
            bool flag = false;

            OracleParameter[] parms = new OracleParameter[] { 
              new OracleParameter("p_pid",model.PID),
              new OracleParameter("p_SUBMITDEPART",model.SUBMITDEPART),
              new OracleParameter("p_submituser",model.SUBMITUSER),
              new OracleParameter("p_auditstatus",model.AUDITSTATUS),
              new OracleParameter("p_touser",model.TOUSER),
              new OracleParameter("p_remark",model.REMARK),
              new OracleParameter("returnvalue",OracleType.Int32)
            };

            for (int i = 0; i < 6; i++)
                parms[i].Direction = ParameterDirection.Input;
            parms[6].Direction = ParameterDirection.Output;

            try
            {
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_audit.proc_specail_bmaudit", parms);

                if (parms[6].Value != DBNull.Value && OracleHelper.ToInt(parms[6].Value.ToString()) == 1)
                    flag = true;
            }
            catch (Exception ex)
            {
                log.ExceptionHand(ex);
            }

                //--特批部门经理审核
                //procedure proc_specail_bmaudit(
                // p_pid in number,
                // p_SUBMITDEPART in varchar2,
                // p_submituser in varchar2,
                // p_auditstatus in integer,
                // p_touser in integer,
                // p_remark in varchar2,
                // returnvalue out integer--0失败，1成功
                //)

                return flag;
        }

		#endregion  Method
	}
}

